package com.commerce.controller.back;

import com.commerce.constant.UserType;
import com.commerce.model.Admin;
import com.commerce.service.AdminService;
import com.commerce.util.MDS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


/**
 * @author wyj
 * @Title:
 * @Description:
 */
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录页面
     * @return
     */
    @RequestMapping(value="/login" , method = RequestMethod.GET)
    public String login(HttpSession session){
        if(session.getAttribute(UserType.ADMINUSET.name()) != null)
            return "redirect:/admin/index";
        return "back/login";
    }


    /**
     * 登录验证
     * @param admin
     * @param httpSession
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login" , method = RequestMethod.POST)
    public String selectLogin(Admin admin, HttpSession httpSession){
        admin.setPassword(MDS.encryptBasedDes(admin.getAccount()+admin.getPassword()));
        return  adminService.login(admin , httpSession);
    }

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "index" , method = RequestMethod.GET)
    public String index(){
        return "back/index";
    }

    /**
     * 退出
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "exit" , method = RequestMethod.GET)
    public String exit(HttpSession httpSession){
        httpSession.invalidate();
        httpSession=null;
        return "redirect:/admin/login";
    }
}
